package ui.list 
{
	import com.GCItem;
	import data.GCItemData;
	import flash.display.Shape;
	import ui.textfield.GCTextField;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * ...
	 * @author Jason Won
	 */
	public class GCListUI extends Sprite
	{
		private var _reference:GCItem;			// Reference to actual object
		private var _isSale:Boolean;			// Boolean for sales
		private var _quantityText:GCTextField;	// Text for item quantity
		private var _nameText:GCTextField;		// Text for item name
		private var _priceText:TextField; 		// Text for item price
		
		private var _quantityBack:Shape;
		private var _selectedScreen:Shape;
		private var _uiWidth:int = 250;
		private var _uiHeight:int = 50;
		
		/**
		 * Constructor
		 * @param	item
		 * @param	isSale
		 */
		public function GCListUI(item:GCItem)		
		{
			_reference = item;
			_isSale = item.isSale;
			_quantityText = new GCTextField();
			_nameText = new GCTextField();
			
			// Add background image
			var background:Bitmap = GCResources.getWoodBackground(_uiWidth, _uiHeight);
			this.addChild(background);
			
			// Add the image of the item
			var itemImage:Bitmap = (GCItemData.ITEM_TYPE_RECIPE == GCItemData.getTypeFromId(item.itemId)) ?
				GCResources.getResource("recipe") : GCResources.getResource(GCItemData.getNameFromId(item.itemId));
			this.addChild(itemImage);
			
			// Add the quantity text
			_quantityBack = new Shape();
			_quantityBack.graphics.beginFill(0x000000);
			_quantityBack.graphics.drawRoundRect(0, 0, 22, 22, 10);
			_quantityBack.graphics.endFill();
			_quantityBack.alpha = .8;
			this.addChild(_quantityBack);
			_quantityText.setStyle(GCTextField.TEXT_TYPE_QUEST);
			_quantityText.textColor = GCConstants.SALES_TEXT_COLOR;
			_quantityText.text = item.quantity.toString();
			_quantityText.selectable = false;
			_quantityText.width = _quantityBack.width;
			_quantityText.multiline = false;
			this.addChild(_quantityText);
			
			// Add the name text
			_nameText.textColor = GCConstants.SALES_TEXT_COLOR;
			_nameText.text = "Name: " + GCItemData.getNameFromId(item.itemId);
			_nameText.x = itemImage.x + itemImage.width + 10;
			_nameText.height = 20;
			_nameText.selectable = false;
			this.addChild(_nameText);
			
			// Add additional text for sales
			if (_isSale)
			{
				_priceText = new TextField();
				_priceText.textColor = GCConstants.SALES_TEXT_COLOR;
				_priceText.text = "Price Per Unit: " + item.price.toString();
				_priceText.x = _nameText.x;
				_priceText.y = _nameText.y + _nameText.height + 10;
				_priceText.autoSize = TextFieldAutoSize.LEFT;
				_priceText.selectable = false;
				this.addChild(_priceText);
			}
			
			_selectedScreen = new Shape();
			_selectedScreen.graphics.beginFill(0x0000ff);
			_selectedScreen.graphics.drawRect(0, 0, _uiWidth, _uiHeight);
			_selectedScreen.graphics.endFill();
			_selectedScreen.alpha = .5;
		}
		
		
		//----------------------------------------------------------------------
		//	PUBLIC FUCTIONS
		//----------------------------------------------------------------------		
		
		public function selected():void
		{
			this.addChild(_selectedScreen);
		}
		
		public function deselected():void
		{
			if (_selectedScreen.parent == this)
				this.removeChild(_selectedScreen);
		}
		
		public function changeQuantity(newQuantity:int):void
		{
			_quantityText.text = newQuantity.toString();
		}
		
		public function changePrice(newPrice:int):void
		{
			if (_isSale)
			{
				_priceText.text = "Price: " + newPrice.toString();
			}
		}
		
		//----------------------------------------------------------------------
		//	GET FUNCTIONS
		//----------------------------------------------------------------------	
		
		public function get reference():GCItem
		{
			return _reference;
		}
		
	}
}